<template>
  <section class="tags">
    <el-tag
      :key="tag"
      v-for="tag in intents"
      closable
      :disable-transitions="false"
      @close="removeIntent(tag)">
      {{tag}}
    </el-tag>

    <el-input
      class="input-new-tag"
      v-if="inputVisible"
      v-model="inputValue"
      ref="saveTagInput"
      size="small"
      @keyup.enter.native="handleInputConfirm"
      @blur="handleInputConfirm"
    >
    </el-input>
    <el-button v-else class="button-new-tag" size="small" @click="showInput">+ New Tag</el-button>
  </section>
</template>

<script>
  import {mapState} from 'vuex'
  export default {
    data () {
      return {
        inputVisible: false,
        inputValue: ''
      }
    },
    computed: mapState('intents', ['intents']),
    methods: {
      removeIntent(intent) {
        this.$store.commit('intents/removeIntent', intent)
      },
      showInput() {
        this.inputVisible = true;
        this.$nextTick(_ => {
          this.$refs.saveTagInput.$refs.input.focus()
        })
      },
      handleInputConfirm() {
        let inputValue = this.inputValue
        if (inputValue) {
          this.$store.commit('intents/addIntent', inputValue)
        }
        this.inputVisible = false
        this.inputValue = ''
      }
    }
  }
</script>

<style scoped>
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 200px;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>
